package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * ClassName: AddressBookController
 * Package: com.sky.controller.user
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/10/13 14:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @return
     */
    @PostMapping
    public Result addLocation(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.addLocation(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public Result defaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认地址的ID为：{}",addressBook);
        addressBookService.defaultAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址：{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        log.info("根据id查询地址：{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteAddressById(@RequestParam Long id){
        log.info("删除地址的ID为:{}");
        addressBookService.delete(id);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("list")
    public Result<List<AddressBook>> list(){
        log.info("查询当前登录用户的所有地址信息");
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .build();
      List<AddressBook> addressBookList = addressBookService.list(addressBook);
      return Result.success(addressBookList);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    public Result<List<AddressBook>> defaultAddress(){
        log.info("查询当前用户的默认地址");
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .build();
        List<AddressBook>  addressBookList = addressBookService.defaultAddressquery(addressBook);
        return Result.success(addressBookList);
    }
}
